package pl.edu.mimuw.irs.rcp.controller.editor.report;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import pl.edu.mimuw.irs.core.model.analysis.History;
import pl.edu.mimuw.irs.core.model.analysis.Report;
import pl.edu.mimuw.irs.core.model.simulation.Calculations;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;

/**
 * @author Jakub Rauch
 * Created on: 2009-05-25
 */
public class ReportEditor extends EditorPart implements PropertyChangeListener {

	public static final String ID = "pl.edu.mimuw.irs.editor.report";
	public static final String COMMAND_ID = "pl.edu.mimuw.irs.command.editreport";
	private static Integer MAX_CHART_VALUES = 10;

	// *** Attributes
	
	private Chart totalCostChart;
	private Chart totalTimeChart;
	private Chart resourcesCostChart;
	private Chart resourcesTimeChart;
	private ResultsTable resultsTable;
	@SuppressWarnings("unused")
	private StepsTable stepsTable;
	private Report model;
	private ISelectionListener listener = new ISelectionListener() {
		@Override
		public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
			// we ignore our own selections
			if (sourcepart != ReportEditor.this) {
			    //showSelection(sourcepart, selection);
			}
		}
	};
	
	// *** Constructors

	public ReportEditor() {
		super();
	}

	// *** Methods

	@Override
	public void doSave(IProgressMonitor monitor) {
		// XXX : container editor save
	}

	@Override
	public void doSaveAs() {
		
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		this.model = (((ReportEditorInput) input).getReport());
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		//GridData gridData;
		
	    // create the desired layout for this wizard page
		FillLayout rootLayout = new FillLayout();
		rootLayout.type = SWT.VERTICAL;
		parent.setLayout(rootLayout);

		model.addPropertyChangeListener(this);
		final List<History> list = model.getCheapest();
		//GridData gridData;
		
		/* Prepare data */
		
		Calculations calculations = new Calculations(list, MAX_CHART_VALUES, Calculations.CALCULATE_RESOURCES);
		
		/* Create total charts */
		
		Group totalCharts = new Group(parent, SWT.NONE);
		totalCharts.setText(UITexts.get(MessageCode.REPORT_EDITOR_CHART_TOTAL_GROUP));
		Layout fillLayout = new FillLayout();
		totalCharts.setLayout(fillLayout);
		
		totalCostChart = new Chart(totalCharts, Chart.LINE,
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_COST_TOTAL_TITLE),
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_COST_TOTAL_DOM),
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_COST_TOTAL_VAL));
		totalCostChart.loadValues(calculations.getAvgCosts());
		totalTimeChart = new Chart(totalCharts, Chart.LINE, 
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_TIME_TOTAL_TITLE),
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_TIME_TOTAL_DOM),
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_TIME_TOTAL_VAL));
		totalTimeChart.loadValues(calculations.getAvgTimes());
		
		/* Create resources usage charts */
		
		Group resourcesCharts = new Group(parent, SWT.NONE);
		resourcesCharts.setText(UITexts.get(MessageCode.REPORT_EDITOR_CHART_RESOURCES_GROUP));
		fillLayout = new FillLayout();
		resourcesCharts.setLayout(fillLayout);
		
		resourcesCostChart = new Chart(resourcesCharts, Chart.LINE,
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_COST_RESOURCES_TITLE),
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_COST_RESOURCES_DOM),
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_COST_RESOURCES_VAL));
		resourcesCostChart.loadValues(calculations.getResourcesAvgCosts());
		resourcesTimeChart = new Chart(resourcesCharts, Chart.LINE,
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_TIME_RESOURCES_TITLE),
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_TIME_RESOURCES_DOM),
				UITexts.get(MessageCode.REPORT_EDITOR_CHART_TIME_RESOURCES_VAL));
		resourcesTimeChart.loadValues(calculations.getResourcesAvgTimes());
		
		/* Create table */

		Group details = new Group(parent, SWT.NONE);
		details.setText(UITexts.get(MessageCode.REPORT_EDITOR_CHART_DETAILS_GROUP));
		fillLayout = new FillLayout();
		details.setLayout(fillLayout);
		
		resultsTable = new ResultsTable(details);
		getSite().setSelectionProvider(resultsTable.getViewer());
		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(listener);
		stepsTable = new StepsTable(details, resultsTable);
		
		resultsTable.loadHistory(list);
		
		/* Other */

		long diff = (model.getEnd().getTime() - model.getStart().getTime());
		setContentDescription("Generation (" + list.size() + 
				" successful runs) took : " + 
				(diff / 1000) + "," + 
				(diff % 1000) + " secs;");
	}

	@Override
	public void dispose() {
		model.removePropertyChangeListener(this);
		super.dispose();
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		resultsTable.getViewer().getControl().setFocus();
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		
		setContentDescription("Currently applicable runs: " + model.getCustomRuns().size());

		resultsTable.loadHistory(model.getCustomRuns());
		
		if (model.getCustomRuns().size() == 0) {
			totalCostChart.loadValues(new Integer[]{0});
			totalTimeChart.loadValues(new Integer[]{0});
			resourcesCostChart.loadValues(new Integer[]{0});
			resourcesTimeChart.loadValues(new Integer[]{0});
		} else {
			Calculations calculations = new Calculations(model.getCustomRuns(), MAX_CHART_VALUES, Calculations.CALCULATE_RESOURCES);
			
			totalCostChart.loadValues(calculations.getAvgCosts());
			totalTimeChart.loadValues(calculations.getAvgTimes());
			resourcesCostChart.loadValues(calculations.getResourcesAvgCosts());
			resourcesTimeChart.loadValues(calculations.getResourcesAvgTimes());
		}
	}
	
}
